import random

import requests
import logging
import config


class DepartmentAPI:

    #添加部门接口
    @classmethod
    def add_department(cls,token,body):
        url= config.monitor_domain+"/system/dept"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers={"content-type":"application/json","Authorization":token,"Uuidstr":uuid}
        resp = requests.post(url=url,headers=headers,json=body)
        logging.info(f"add_department url:{url}")
        logging.info(f"add_department body:{body}")
        logging.info(f"add_department resp:{resp.text}")
        return resp
        pass

    #获取部门列表
    @classmethod
    def query_department(cls,token,param):
        url =config.monitor_domain+"/system/dept/list"
        # parameter={"deptName":config.deptName}
        headers = {"Authorization": token}
        resp =requests.get(url=url,params=param,headers=headers)
        logging.info(f"query_department url:{url}")
        logging.info(f"query_department resp:{resp.text}")
        return resp

    #修改部门
    @classmethod
    def change_department(cls,token,body):
        url =config.monitor_domain+"/system/dept"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers={"content-type":"application/json","Authorization": token,"Uuidstr":uuid}
        logging.info(f"change_department headers:{headers}")
        resp =requests.put(url=url,headers=headers,json=body)
        logging.info(f"change_department url:{url}")
        logging.info(f"change_department resp:{resp.text}")
        return resp

    #删除部门
    @classmethod
    def delete_department(cls,token,deptId):
        url = config.monitor_domain+f"/system/dept/{deptId}"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr":uuid}
        resp = requests.delete(url=url,headers=headers)
        logging.info(f"delete_department url:{url}")
        logging.info(f"delete_department headers:{headers}")
        logging.info(f"delete_department resp:{resp.text}")
        return resp
    #查询部门列表（排除节点）
    @classmethod
    def exclude_deptId(cls,token,deptId):
        url = config.monitor_domain + f"/system/dept/list/exclude/{deptId}"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        resp = requests.get(url=url, headers=headers)
        logging.info(f"exclude_deptId url:{url}")
        logging.info(f"exclude_deptId headers:{headers}")
        logging.info(f"exclude_deptId resp:{resp.text}")
        return resp

    # 根据部门编号获取详细信息
    @classmethod
    def get_detailed_by_deptId(cls, token, deptId):
        url = config.monitor_domain + f"/system/dept/{deptId}"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        resp = requests.get(url=url, headers=headers)
        logging.info(f"get_detailed_by_deptId url:{url}")
        logging.info(f"get_detailed_by_deptId headers:{headers}")
        logging.info(f"get_detailed_by_deptId resp:{resp.text}")
        return resp
    #获取部门下拉树列表
    @classmethod
    def get_tree_select(cls, token):
        url = config.monitor_domain + "/system/dept/treeselect"
        uuid = "a9f25c87-" + str(random.randint(1000, 9999))
        headers = {"Authorization": token, "Uuidstr": uuid}
        param = {"deptName":config.api_deptName}
        resp = requests.get(url=url, headers=headers,params=param)
        logging.info(f"get_tree_select url:{url}")
        logging.info(f"get_tree_select headers:{headers}")
        logging.info(f"get_tree_select resp:{resp.text}")
        return resp


